using System;
using System.Collections.Generic;
using System.Text;

namespace LoanPrequalificationSystem.Model.LoanApplications
{
    public class Offer
    {
        private DateTime _expirationDate;
        private int _loanAmount;
        private float _interestRate;
        private int _loanTerm;

        public Offer()
        {
        }
        public Offer(int loanAmount, int loanTerm, float interestRate, DateTime expirationDate)
        {
            _loanAmount = loanAmount;
            _loanTerm = loanTerm;
            _interestRate = interestRate;
            _expirationDate = expirationDate;
            if (GetBrokenRules().Count > 0)
            {
                throw new Exception(String.Format("The offer is trying to be created with invalid data. {0}",
                                                  GetBrokenRulesToString()));
            }
        }



        public DateTime expirationDate
        {
            get { return _expirationDate; }
        }
        public int LoanAmount
        {
            get { return _loanAmount; }
        }
        public float InterestRate
        {
            get { return _interestRate; }
        }
        public int LoanTerm
        {
            get { return _loanTerm; }
        }
        public bool HasExpired()
        {
            return expirationDate < DateTime.Now.AddMonths(-6);
        }
        private string GetBrokenRulesToString()
        {
            var sbBrokenRules = new StringBuilder();

            foreach (var brokenRule in GetBrokenRules())
            {
                sbBrokenRules.Append(brokenRule.Rule);
            }
            return sbBrokenRules.ToString();
        }

        public List<BrokenBusinessRule> GetBrokenRules()
        {
            var brokenRules = new List<BrokenBusinessRule>();
            if (InterestRate <= 0)
            {
                brokenRules.Add(new BrokenBusinessRule("InterestRate","An offer must have an interest rate greater than zero."));
            }
            if (LoanAmount <= 0)
            {
                brokenRules.Add(new BrokenBusinessRule("LoanAmount","An offer must have a loan amount greater than zero."));
            }
            if (LoanTerm <= 0)
            {
                brokenRules.Add(new BrokenBusinessRule("LoanTerm","An offer must have a loan term greater than zero."));
            }

            return brokenRules;
        }
    }
}